<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增食品</title>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.4/lib/index.css">
    <link rel="stylesheet" href="/css/global.css">

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.bootcss.com/qs/6.5.2/qs.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vant@2.4/lib/vant.min.js"></script>
</head>
<style>
    .van-cell__value {
        overflow: initial;
    }
</style>
<body>
<!--<div class="bg"></div>-->
<div id="app">
    <van-row>
        <van-col span="24">
            <van-nav-bar
                    title="新增/编辑"
                    left-text="返回"
                    left-arrow
                    @click-left="onClickLeft"
            >
                <template #right>
                    <van-button @click="addfood" style="margin: 8px 0;border-radius: 4px" size="small" square
                                color="linear-gradient(to right, #4bb0ff, #6149f6)" block enctype="multipart/form-data">
                        保存
                    </van-button>
                </template>
            </van-nav-bar>
        </van-col>
        <van-col span="24">
            <div class="box"></div>
        </van-col>
        <van-col span="24">
            <van-divider dashed>必填信息</van-divider>
            <van-form @submit="onSubmit" enctype="multipart/form-data">
                <van-field
                        v-model="name"
                        id="name"
                        name="碳水化合物"
                        label="食品名称"
                        :rules="[{ required: true, message: '请填写食物名称' }]"
                ></van-field>
                <van-field
                        id="fmdescription"
                        v-model="fmdescription"
                        rows="2"
                        autosize
                        label="食品描述"
                        type="textarea"
                        maxlength="50"
                        placeholder="请输入描述"
                        show-word-limit
                ></van-field>
                <div class="bg-f9 p-3" style="position: relative" v-for="(auint,index) in funits">
                    <button class="bg-f9 color-main border-0" @click="deleteAUnit(index)">删除</button>
                    <van-field
                            name="单位名称"
                            label="单位名称"
                            placeholder="单位名称"
                            v-model="auint.unitName"
                            :rules="[{ required: true, message: '请填写单位名称' }]"
                    >{{auint.uinitName}}
                    </van-field>

                    <van-field
                            name="热量"
                            label="热量"
                            placeholder="例如（1000千卡）"
                            v-model="auint.unitConsumeCalories"
                            :rules="[{ required: true, message: '请填写热量' }]"
                    >{{auint.uintConsumeCalories}}
                    </van-field>
                    <!--                    <van-field-->
                    <!--                            readonly-->
                    <!--                            clickable-->
                    <!--                            name="picker"-->
                    <!--                            :value="value"-->
                    <!--                            label="选择单位"-->
                    <!--                            placeholder="请选择"-->
                    <!--                            @click="showPicker = true"-->
                    <!--                    >-->
                    <!--                    </van-field>-->
                    <!--                    <van-popup v-model="showPicker" position="bottom">-->
                    <!--                        <van-picker-->
                    <!--                                show-toolbar-->
                    <!--                                :columns="columns"-->
                    <!--                                @confirm="onConfirm"-->
                    <!--                                @cancel="showPicker = false"-->
                    <!--                        />-->
                    <!--                    </van-popup>-->
                </div>
                <!--                    <van-field-->
                <!--                            readonly-->
                <!--                            clickable-->
                <!--                            name="picker"-->
                <!--                            :value="value"-->
                <!--                            label="选择单位"-->
                <!--                            placeholder="请选择"-->
                <!--                            @click="showPicker = true"-->
                <!--                    >-->
                <!--                    </van-field>-->
                <van-popup v-model="showPicker" position="bottom">
                    <van-picker
                            show-toolbar
                            :columns="columns"
                            @confirm="onConfirm"
                            @cancel="showPicker = false"
                    />
                </van-popup>
                <div class="pt-3">
                    <van-button color="#349aff" plain style="border-radius: 4px" @click="addAUnit" square block>新增单位
                    </van-button>
                </div>

            </van-form>
        </van-col>
        <van-col span="24">
            <van-divider dashed>选填信息</van-divider>
            <!--            文件上传-->
            <van-uploader v-model="fileList" multiple :max-count="1" max-size='500*500' :after-read="afterRead">
                <van-button icon="plus" type="primary">点我上传图片</van-button>
                <template #preview-cover="{ file }">
                    <div class="preview-cover van-ellipsis">{{ file.name }}</div>
                </template>
            </van-uploader>

            <!--            测试文件上传-->
            <!--            <van-uploader v-model="fileList" multiple :max-count="1" max-size='500*500' :after-read="afterRead" >-->
            <!--                <van-button icon="plus" type="primary">测试上传文件</van-button>-->
            <!--                <template #preview-cover="{ file }">-->
            <!--                    <div class="preview-cover van-ellipsis">{{ file.name }}</div>-->
            <!--                </template>-->

            <!--            </van-uploader>-->
            <!--            <van-button icon="plus" type="primary" @click="testFileUpload">确定上传</van-button>-->
            <style>
                .preview-cover {
                    position: absolute;
                    box-sizing: border-box;
                    bottom: 0;
                    width: 100%;
                    padding: 4px;
                    color: #fff;
                    font-size: 12px;
                    text-align: center;
                    background: rgba(0, 0, 0, 0.3);
                }
            </style>

            <van-form @submit="onSubmit">
                <!--                <van-field name="uploader" label="文件上传">-->
                <!--                    <template #input>-->
                <!--                        <van-uploader v-model="uploader" />-->
                <!--                    </template>-->
                <!--                </van-field>-->
                <van-field
                        id="fat"
                        v-model="fat"
                        name="脂肪"
                        label="脂肪"
                        placeholder="单位:g/100g"
                        :rules="[{ required: true, message: '请填写食物名称' }]"
                ></van-field>
                <van-field
                        id="protein"
                        v-model="protein"
                        name="蛋白质"
                        label="蛋白质"
                        placeholder="单位:g/100g"
                        :rules="[{ required: true, message: '请填写食物名称' }]"
                ></van-field>
                <van-field
                        id="carbohydarete"
                        v-model="carbohydrate"
                        name="碳水化合物"
                        label="脂碳水化合物肪"
                        placeholder="单位:g/100g"
                        :rules="[{ required: true, message: '请填写食物名称' }]"
                ></van-field>
                </van-field>
                <!--                <van-field name="checkboxGroup" label="标签" v-model="tag">-->
                <!--                    <template #input v-model="tag">-->
                <!--                        <van-checkbox-group v-model="checkboxGroup" direction="horizontal">-->
                <!--                            <van-checkbox class="d-inline-flex mr-2" name="1" shape="square">标签1</van-checkbox>-->
                <!--                            <van-checkbox class="d-inline-flex mr-2" name="2" shape="square">标签2</van-checkbox>-->
                <!--                            <van-checkbox class="d-inline-flex mr-2" name="3" shape="square">标签3</van-checkbox>-->
                <!--                            <van-checkbox class="d-inline-flex mr-2" name="4" shape="square">标签4</van-checkbox>-->
                <!--                        </van-checkbox-group>-->
                <!--                    </template>-->
                <!--                </van-field>-->
                <van-field
                        id="ifo"
                        v-model="ifo"
                        rows="5"
                        autosize
                        label="详细描述"
                        type="textarea"
                        maxlength="500"
                        placeholder="请输入详细描述"
                        show-word-limit
                ></van-field>
                <van-field
                        id="ortherNutrient"
                        v-model="ortherNutrient"
                        rows="5"
                        autosize
                        label="其他营养素"
                        type="textarea"
                        maxlength="500"
                        placeholder="请输入其他营养素"
                        show-word-limit
                ></van-field>
            </van-form>
        </van-col>
    </van-row>
</div>
</body>
<script>
    var vm = new Vue({
        el: '#app',
        data() {
            return {
                fileList: [
                    // {
                    //     url: 'https://img.yzcdn.cn/vant/leaf.jpg',
                    //     isImage: true,
                    //     status:'uploading',
                    //     message:'正在上传'
                    // },
                    // Uploader 根据文件后缀来判断是否为图片文件
                    // 如果图片 URL 中不包含类型信息，可以添加 isImage 标记来声明
                    // { url: 'https://cloud-image',
                    //     isImage: true,
                    //     status:'uploading',
                    //     message:'正在上传'},
                ],
                file: '',
                name: '',
                username: '',
                fmdescription: '',
                ifo: '',
                password: '',
                value: '',
                columns: ['个', '份', '瓶'],
                showPicker: false,
                uploader: [{url: 'https://img.yzcdn.cn/vant/leaf.jpg'}],
                checkbox: false,
                checkboxGroup: [],
                message: '',
                tag: '',
                ortherNutrient: '',
                carbohydrate: '',
                protein: '',
                fat: '',
                funits: [
                    {
                        unitName: '',
                        unitConsumeCalories: '',
                    },

                ]
            };
        },
        methods: {
            addAUnit() {
                this.funits.push({
                    unitName: '',
                    unitConsumeCalories: '',
                },);
            },
            afterRead(file) {
                // file.status = 'uploading';
                // file.message = '上传中...';
                vm.$data.file = file;
                // setTimeout(() => {
                //     file.status = 'failed';
                //     file.message = '上传失败';
                // }, 6000);
                console.log(file);
            },
            deleteAUnit(index) {
                this.funits.splice(index, 1);
            },
            onClickLeft() {
                window.location.href = "toLookforfood";
                Toast('返回');
            },
            onSubmit(values) {
                console.log('submit', values);
            },
            onConfirm(value) {
                this.value = value;
                this.showPicker = false;
            },
            testFileUpload() {
                // let headers = {headers: {"Content-Type": "multipart/form-data"}};
                // let postdata=new FormData();
                // postdata.append("file",vm.$data.file);
                debugger
                let postdata = {
                    afile: vm.$data.file,
                };
                let config = {
                    //添加请求头
                    headers: {
                        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
                    }
                };
                console.log("postdata is " + JSON.stringify(postdata));
                /* axios.post(`/fooduser/testFileUpload`,((postdata)),config
                 )*/
                let formData = new FormData();
                formData.append('category', 'settingPic');
                formData.append('afile', vm.$data.file.file);
                axios.post(`/fooduser/testFileUpload`, (formData), config)
                /*this.$axios({
                    method: "POST",
                    url: '/fooduser/testFileUpload',
                    data: formData
                })*/

                    .then(res = > {
                    console.log('res=>', res);
                alert(res.data.returnMsg);
            })
                ;

            },
            addfood() {
                //  将units封装成字符串
                var nameString = "";
                var numString = "";
                for (var i = 0; i < vm.$data.funits.length; i++) {
                    if (i == 0) {
                        nameString += vm.$data.funits[i].unitName;
                        numString += vm.$data.funits[i].unitConsumeCalories;
                    } else {
                        nameString += "@" + vm.$data.funits[i].unitName;
                        numString += "@" + vm.$data.funits[i].unitConsumeCalories;
                    }

                    console.log('the ' + i + ' of nameString is  ' + nameString + 'and the numString is ' + numString);
                }
                console.log('******Finally the string  of nameString is  ' + nameString + 'and the numString is ' + numString);
                // let postdata ={
                //     name: this.name,
                //     fmdescription: this.fmdescription,
                //     ifodescroption: this.ifo,
                //     ortherNutrient: this.ortherNutrient,
                //     carbohydrate: this.carbohydrate,
                //     protein:this.protein,
                //     fat:this.fat,tag:this.tag,
                //     // funit :this.funits,
                //     nameString:nameString,
                //     numString:numString,
                //     file:this.file
                //  };
                const postdata = new FormData();
                postdata.append('name', this.name);
                postdata.append('fmdescription', this.fmdescription);
                postdata.append('ifodescroption', this.ifo);
                postdata.append('ortherNutrient', this.ortherNutrient);
                postdata.append('carbohydrate', this.carbohydrate);
                postdata.append('protein', this.protein);
                postdata.append('fat', this.fat);
                postdata.append('tag', this.tag);
                postdata.append('numString', numString);
                postdata.append('nameString', nameString);
                postdata.append('afile', this.file.file);
                let headers = {headers: {"Content-Type": "multipart/form-data"}};
                console.log("the postdata is " + JSON.stringify(postdata));
                axios.post(`/fooduser/addAUnuploadedFoodModel`, ((postdata)))
                    .then(res = > {
                    console.log('res=>', res);
                alert(res.data.returnMsg);
            })
                ;
            }
        },

    });
</script>
</html>